<?php
namespace app\admin\controller;

use app\admin\model\Admin;
use app\common\controller\Backend;
use app\admin\model\QuesMerchant;
use app\admin\model\QuesTeacher;
use app\admin\model\QuesStudent;
use app\admin\model\College;
use app\admin\model\Classes;
use think\Db;
use think\helper\Time;
use think\Config;

class Datacounttest extends Backend
{

    protected $remodel = null;
    protected $where = [];
    private $ques = ['a_1','a_2','a_3','a_4','a_5','a_6'];
    public function _initialize()
    {
        parent::_initialize();
        $gtypeid =  $this->auth->gtypeid;
        $gid = $this->auth->gid;
        if($gtypeid == 1)
        {
            // $this->remodel = new College();
            $this->where['college_id'] =  $gid;
        }
        if($gtypeid == 3)
        {
            $this->where['classes_id'] =  $gid;
            // $this->remodel = new Classes();
        }
        $timestampArr = Time::today();
        $this->where['create_time'] = ['between', "$timestampArr[0], $timestampArr[1]"];


    }
    public function studentcnt(){

        $classesid = $this->auth->gid;
        $student = new QuesStudent();
        $total = $student->where($this->where)->count();

        $ret = $student->where($this->where)->select();
        // echo $student->getLastSql();
        echo "<pre>";
        $data = [];
        foreach($ret as $key => $value){
            // print_r($value->toArray());
            $value = $value->toArray();
            foreach ($value as $k => $v) {
                // echo $k;

                if(in_array($k, $this->ques))
                {
                    if(!isset($data[$k]))
                    {
                        $data[$k] = config("ques.{$k}");
                    }
                    $data[$k]["answ"][$v]["num"] ++;

                }
            }

        }

        print_r($data);
        // echo "<pre>";


    }

    public function teachercnt(){
        $teacher = new QuesTeacher();
        $total = $teacher->where($this->where)->count();


        $rets1 = $teacher->field('a_1, count(1) cnt')->where($this->where)->group('a_1')->select();
        foreach($rets1 as $ret){
            if(in_array($ret['a_1'], array('1','2','3','4','5','6','7','0'))){
                $this->arr1[$ret['a_1']]['num'] = $ret['cnt'];
                $this->arr1[$ret['a_1']]['rate'] =  round(($ret['cnt'] / $total)*100,2);
            }
        }

        $rets2 = $teacher->field('a_2, count(1) cnt')->where($this->where)->group('a_2')->select();
        foreach($rets2 as $ret){
            if(in_array($ret['a_2'], array('1','2','0'))){
                $this->arr2[$ret['a_2']]['num'] = $ret['cnt'];
                $this->arr2[$ret['a_2']]['rate'] =  round(($ret['cnt'] / $total)*100,2);
            }
        }

        $rets3 = $teacher->field('a_3, count(1) cnt')->where($this->where)->group('a_3')->select();
        foreach($rets3 as $ret){
            if(in_array($ret['a_3'], array('1','2','3','0'))){
                $this->arr3[$ret['a_3']]['num'] = $ret['cnt'];
                $this->arr3[$ret['a_3']]['rate'] =  round(($ret['cnt'] / $total)*100,2);
            }
        }

        $rets4 = $teacher->field('a_4, count(1) cnt')->where($this->where)->group('a_4')->select();
        foreach($rets4 as $ret){
            if(in_array($ret['a_4'], array('1','2','3','4','5','6','0'))){
                $this->arr4[$ret['a_4']]['num'] = $ret['cnt'];
                $this->arr4[$ret['a_4']]['rate'] =  round(($ret['cnt'] / $total)*100,2);
            }
        }

        $rets5 = $teacher->field('a_5, count(1) cnt')->where($this->where)->group('a_5')->select();
        foreach($rets5 as $ret){
            if(in_array($ret['a_5'], array('1','2','0'))){
                $this->arr5[$ret['a_5']]['num'] = $ret['cnt'];
                $this->arr5[$ret['a_5']]['rate'] =  round(($ret['cnt'] / $total)*100,2);
            }
        }
         $rets6 = $teacher->field('province,count(*) as total')
                ->where($this->where)
                ->group('province_code')
                ->order('total','desc')
                ->select();
        $rets6 = collection($rets6)->toArray();
        $rets6_count = $teacher->field('province,count(*) as total')
                ->where($this->where)
                ->group('province_code')
                ->count();
        $this->assign('total', $total);
        $this->assign('arr1', $this->arr1);
        $this->assign('arr2', $this->arr2);
        $this->assign('arr3', $this->arr3);
        $this->assign('arr4', $this->arr4);
        $this->assign('arr5', $this->arr5);
        $this->assign('arr6', $rets6);
        $this->assign('arr6_count', $rets6_count);
        return $this->fetch();
    }


    public function merchantcnt(){
        $merchant = new QuesMerchant();
        $total = $merchant->count();
        $rets1 = $merchant->field('a_1, count(1) cnt')->group('a_1')->select();

        foreach($rets1 as $ret){
            if(in_array($ret['a_1'], array('1','2','3','4','5','6','7','0'))){
                $this->arr1[$ret['a_1']]['num'] = $ret['cnt'];
                $this->arr1[$ret['a_1']]['rate'] =  round(($ret['cnt'] / $total)*100,2);
            }
        }

        $rets2 = $merchant->field('a_2, count(1) cnt')->group('a_2')->select();
        foreach($rets2 as $ret){
            if(in_array($ret['a_2'], array('1','2','0'))){
                $this->arr2[$ret['a_2']]['num'] = $ret['cnt'];
                $this->arr2[$ret['a_2']]['rate'] =  round(($ret['cnt'] / $total)*100,2);
            }
        }

        $rets3 = $merchant->field('a_3, count(1) cnt')->group('a_3')->select();
        foreach($rets3 as $ret){
            if(in_array($ret['a_3'], array('1','2','3','0'))){
                $this->arr3[$ret['a_3']]['num'] = $ret['cnt'];
                $this->arr3[$ret['a_3']]['rate'] =  round(($ret['cnt'] / $total)*100,2);
            }
        }

        $rets4 = $merchant->field('a_4, count(1) cnt')->group('a_4')->select();
        foreach($rets4 as $ret){
            if(in_array($ret['a_4'], array('1','2','3','4','5','6','0'))){
                $this->arr4[$ret['a_4']]['num'] = $ret['cnt'];
                $this->arr4[$ret['a_4']]['rate'] =  round(($ret['cnt'] / $total)*100,2);
            }
        }

        $rets5 = $merchant->field('a_5, count(1) cnt')->group('a_5')->select();
        foreach($rets5 as $ret){
            if(in_array($ret['a_5'], array('1','2','0'))){
                $this->arr5[$ret['a_5']]['num'] = $ret['cnt'];
                $this->arr5[$ret['a_5']]['rate'] =  round(($ret['cnt'] / $total)*100,2);
            }
        }

        $this->assign('arr1', $this->arr1);
        $this->assign('arr2', $this->arr2);
        $this->assign('arr3', $this->arr3);
        $this->assign('arr4', $this->arr4);
        $this->assign('arr5', $this->arr5);
        return $this->fetch();
    }

    /**
     * 数据统计 admin
     */
    public function dataanaadm(){
        if($this -> request -> isPost()){
            $date = $this -> request -> post('create_time');
            $timestamp = strtotime($date);
            $timeArr = [$timestamp,$timestamp+(24*3600)-1];

            $this -> assign('date',$date);
        }else{
            $timeArr = Time::today();
            $this -> assign('date',date('Y-m-d'));
        }
        $admin = Admin::get($this -> auth -> id);

        if(is_null($admin -> gtypeid)){
            $reportedList = Db::name('ques_student')
                -> whereBetween('create_time',$timeArr)
                -> field('count(id) as count,college_name,college_id')
                -> group('college_id')
                -> select();

            $reportedArr = [];
            foreach ($reportedList as $k => $v){
                $reportedArr[$v['college_id']] = $v;
            }

            $studentList =
                Db::name('student')
                -> alias('a')
                -> join('classes b','a.classes_id=b.id')
                -> field('count(a.id) as count,b.college_id as college_id')
                -> group('b.college_id')
                -> select();

            $studentArr = [];
            foreach ($studentList as $k => $v){
                $studentArr[$v['college_id']] = $v;
            }

            $college_ids = (new \app\admin\model\CollegeReport) -> where('type',1) -> whereBetween('create_time',$timeArr) -> column('college_id');

            $colleges = Db::name('college') -> select();
            $collegesStu = $colleges;
            foreach ($collegesStu as $k => &$v){
                $v['allcount'] = isset($studentArr[$v['id']]) ? $studentArr[$v['id']]['count'] : 0;
                $v['repcount'] = isset($reportedArr[$v['id']]) ? $reportedArr[$v['id']]['count'] : 0;
                $v['uncount'] = $v['allcount'] - $v['repcount'];
                $v['is_report'] = in_array($v['id'],$college_ids) ? '<span>已上报</span>':'<span class="text-red">未上报</span>';
                if($v['allcount'] == 0){
                    unset($collegesStu[$k]);
                }
            }


            unset($v);


            $reportedTeaList =  Db::name('ques_teacher')
                -> whereBetween('create_time',$timeArr)
                -> field('count(id) as count,college_name,college_id')
                -> group('college_id')
                -> select();

            $reportedTeaArr = [];
            foreach ($reportedTeaList as $k => $v){
                $reportedTeaArr[$v['college_id']] = $v;
            }

            $teacherList =
                Db::name('teacher')
                    -> alias('a')
                    -> join('department b','a.department_id=b.id')
                    -> field('count(a.id) as count,b.college_id as college_id')
                    -> group('b.college_id')
                    -> select();

            $teacherArr = [];
            foreach ($teacherList as $k => $v){
                $teacherArr[$v['college_id']] = $v;
            }


            $college_ids = (new \app\admin\model\CollegeReport) -> where('type',2) -> whereBetween('create_time',$timeArr) -> column('college_id');

            $collegesTea = $colleges;
            foreach ($collegesTea as $k => &$v){
                $v['allcount'] = isset($teacherArr[$v['id']]) ? $teacherArr[$v['id']]['count'] : '0';
                $v['repcount'] = isset($reportedTeaArr[$v['id']]) ? $reportedTeaArr[$v['id']]['count'] : 0;
                $v['uncount'] = $v['allcount'] - $v['repcount'];
                $v['is_report'] = in_array($v['id'],$college_ids) ? '<span>已上报</span>':'<span class="text-red">未上报</span>';
            }


            $todayStr = date("Y-m-d H:i:s",$timeArr[0]).' - '.date("Y-m-d H:i:s",$timeArr[1]);
            $this -> assign('today',$todayStr);
            $this -> assign('teacherlist',$collegesTea);
            $this -> assign('studentlist',$collegesStu);
            return $this->fetch('dataana_admin');
        }
    }

    /**
     * 学院级别登陆统计
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function dataanacol(){

        if($this -> request -> isPost()){
            $date = $this -> request -> post('create_time');
            $timestamp = strtotime($date);
            $timeArr = [$timestamp,$timestamp+(24*3600)-1];

            $this -> assign('date',$date);
        }else{
            $timeArr = Time::today();
            $this -> assign('date',date('Y-m-d'));
        }

        $admin = Admin::get($this -> auth -> id);
        if($admin -> gtypeid == 1 && $admin -> gid){ //学院级别登陆
            $reportedList = Db::name('ques_student')
                -> whereBetween('create_time',$timeArr)
                -> where('college_id',$admin -> gid)
                -> field('count(id) as count,classes_id')
                -> group('classes_id')
                -> select();

            $reportedArr = [];
            foreach ($reportedList as $k => $v){
                $reportedArr[$v['classes_id']] = $v;
            }

            $studentList =
                Db::name('student')
                    -> alias('a')
                    -> join('classes b','a.classes_id=b.id and b.college_id='.$admin -> gid)
                    -> field('count(a.id) as count,a.classes_id as classes_id')
                    -> group('a.classes_id')
                    -> select();


            $studentArr = [];
            foreach ($studentList as $k => $v){
                $studentArr[$v['classes_id']] = $v;
            }


            $classes = Db::name('classes') -> where('college_id',$admin->gid) -> select();


            $collegesStu = $classes;
            foreach ($collegesStu as $k => &$v){
                $v['allcount'] = isset($studentArr[$v['id']]) ? $studentArr[$v['id']]['count'] : '0';
                $v['repcount'] = isset($reportedArr[$v['id']]) ? $reportedArr[$v['id']]['count'] : 0;
                $v['uncount'] = $v['allcount'] - $v['repcount'];
            }


            unset($v);


            $this -> assign('studentlist',$collegesStu);
            return $this -> fetch('dataana_college');
        }
    }
}